package cn.com.surker.system.controller;

import cn.com.surker.common.AESCoder;
import cn.com.surker.common.AbstractController;
import cn.com.surker.common.Constant;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.system.entity.*;
import cn.com.surker.system.model.CacheUser;
import cn.com.surker.system.service.DataSourceService;
import cn.com.surker.system.service.IErpOriginService;
import cn.com.surker.system.service.OriginPositionService;
import cn.com.surker.system.service.UserService;
import cn.com.surker.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author lucky
 * @since 2021-02-22
 */

@RestController
@RequestMapping("/sys_user")
public class SysUserController extends AbstractController {

    @Resource
	private UserService userService;

    /**
     * 查询列表
     **/
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public RestResultDto findAll(String username, String name, Integer status, Integer offset, Integer limit) {
        try {
            Map<String, Object> map = new HashMap<>();
            if (!StringUtils.isNull(username))
                map.put("username", username);
            if (!StringUtils.isNull(name))
                map.put("name", name);
            if (!StringUtils.isNull(status))
                map.put("status", status);
            //默认第一页 每页显示10条
			return RestResultDto.success(userService.findByPage(username, name, status, offset, limit));
        } catch (Exception e) {
            e.printStackTrace();
            return RestResultDto.fail("系统异常,更新数据失败");
        }
    }

	@RequestMapping(value = "/query/findAllUser", method = RequestMethod.POST)
	@ResponseBody
	public RestResultDto findAllUser(SysUser dto){
		List<SysUser> list = userService.findAll(dto);
		if (list == null || list.size() == 0)
			return RestResultDto.fail("未获取到任何数据");
		return RestResultDto.success(list);
	}

	/**
	 * 清除用户缓存
	 * @return
	 */
	@RequestMapping(value = "/clear/cache", method = RequestMethod.POST)
	@ResponseBody
	public RestResultDto clearCache(){
		CacheUser user = getUser();
		if(user == null){
			return RestResultDto.fail("请先登录");
		}
		user.setRoleList(roleService.loadUserMenu(user.getId()));
		user.setRoleNameList(roleService.loadRoleName(user.getId()));
		redisUtils.set(user.getToken(), user, 7200L);
		AbstractController.setUserCookie(user);
		return RestResultDto.success("缓存已刷新", user);
	}

	/**
	 * 清除用户缓存
	 * @return
	 */
	@RequestMapping(value = "/oauth/token", method = RequestMethod.POST)
	@ResponseBody
	public RestResultDto OAuthToken(){
		CacheUser user = getUser();
		if(user == null) {
			return RestResultDto.fail("请先登录");
		}

//		List<SysOrigin> list = oaOriginService.queryByUidOrigin(user.getId());
//		if (list == null || list.size() == 0)
//			return RestResultDto.fail("未关联站点,请联系管理人员。");

		Map<String, Object> map = new HashMap<>();
		Long time = new Date().getTime();
		String echostr = AESCoder.encrypt("oa" + "_" + user.getUsername() + "_" + time, Constant.centerSecret);

		map.put("username", user.getUsername());
		map.put("oauthType", "oa");
		map.put("timestamp", time);
		map.put("echostr", echostr);
		map.put("token", AESCoder.encrypt(user.getToken(), Constant.CACHE_USER));

		return RestResultDto.success(map);
	}

	/**
	 * 查询调度人员
	 *
	 * @return
	 */
	@RequestMapping(value = "/dispatcher/list", method = RequestMethod.POST)
	@ResponseBody
	public RestResultDto dispatcherList() {
		//	根据岗位查询用户
		List<SysUser> users = userService.queryByOrgAndPosV3(orgId, "调度员");
		return RestResultDto.success(users);
	}

	/**
	 * 加载拌台操作员
	 *
	 * @return
	 */
	@RequestMapping(value = "/mixer/list", method = RequestMethod.POST)
	@ResponseBody
	public RestResultDto mixerList() {
		List<SysUser> users = userService.queryByOrgAndPosV3(orgId, "拌台操作员");
		return RestResultDto.success(users);
	}

	/**
	 * 验证用户是否存在弹票权限
	 *
	 * @return
	 */
	@RequestMapping(value = "/pop/receipt", method = RequestMethod.POST)
	@ResponseBody
	public RestResultDto popReceipt() {
		List<String> roleNameList = getUser().getRoleNameList();
		if (roleNameList.contains("/pop/receipt")) {
			return RestResultDto.success();
		}
		return RestResultDto.fail();
	}

}



